<?php defined('BASEPATH') or exit('No direct script access allowed');

class Authorization_m extends MY_Model {
	function __construct(){
		parent::__construct();
	}
	
	function submit_escort_login(){
		$username = $this->input->post('username', TRUE);
		$password = $this->input->post('password', TRUE);
		$captcha  = $this->input->post('captcha', TRUE);
		
		$correctCaptcha 	= $this->session->userdata('captcha_content');
		$expTime			= $this->session->userdata('captcha_exprise_time');
		$captcha = strtolower($captcha);
		
		if( $correctCaptcha != $captcha){
			echo "captcha_err|".language_translate('authorization_m_captcha_err1');
			exit();
		}
	 	if( time() > $expTime ){
			echo "captcha_err|".language_translate('authorization_m_captcha_err2');
			exit();
		}
		
		$USER = $this->user_entity->init('username', $username);
		if(!$USER){
			echo "username_err|".language_translate('authorization_m_username_err1');
			exit();
		}
		
		if(!$USER->checkPassword($password)){
			echo "password_err|".language_translate('authorization_m_password_err');
			exit();
		}
		
		if($USER->getPermissionSlug() !== USER_TYPE_ESCORT AND $USER->getPermissionSlug() !== USER_TYPE_AGENT){
			echo "username_err|".language_translate('authorization_m_username_err1');
			exit();
		}
		
		if($USER->getStatus() !== 'activated'){
			echo "username_err|".language_translate('authorization_m_username_err2');
			exit();
		}
		
		$session_array = array('account'=> array('user_id'=>$USER->getId() ,'user_obj'=>$USER) );
		$this->session->set_userdata($session_array);
		
		$this->action_log->insertNewAction($USER->getId(),'log_action_login');
		echo 'ok';	
	}
		
	function submit_forgot_details(){
		$email = $this->input->post('email');
		$USER = $this->user_entity->init('email',$email);
		if(!$USER){
			echo json_encode(array('result'=>'err','msg'=>language_translate('escort_login_page_email_error')));
			exit;
		}else {
			$this->email_sender->sendEmailToUser_forgotAccountDetails($USER->getId());
			echo json_encode(array('result'=>'ok','msg'=>language_translate('escort_login_page_send_success')));
			exit;
		}
	}	
		
		
		
		
		
		
//end class	 
}